<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodssortController extends Controller
{
    public function getIndex()
    {
        $cate = self::getCatesByPid(0);
    	$arr = self::getCates(0);
        $r = DB::table('brand')->orderBy('path','desc')->paginate(7);
    	return view('home.goodsort.index',['cate'=>$cate,'arr'=>$arr,'r'=>$r]);
    }

  	//根据pid获取该分类下的所有子分类
    public static function getCatesByPid($gid){
        //获取$pid下的所有子分类
        $res = DB::table('goodsort')->where('gid',$gid)->get();
        $data = [];
        //遍历
        foreach ($res as $k=>$v){
            $v->sub = self::getCatesByPid($v->id);
            $data[] = $v;
        }
        return $data;
    }
    public static function getCates($gid){
        //获取$pid下的所有子分类
        $res = DB::table('brand')->where('gid',$gid)->get();
        $data = [];
        //遍历
        foreach ($res as $k=>$v){
            $v->sub = self::getCates($v->id);
            $data[] = $v;
        }
        return $data;
    }

}
